<?php require 'db.php';?>
<?php
//==========================
$sqlds="SELECT dbo_sinhvien.MASV,dbo_sinhvien.EMAIL";
$sqlds.=" FROM dbo_dondangky, dbo_sinhvien, dbo_doidangky";
$sqlds.=" WHERE dbo_sinhvien.MASV=dbo_dondangky.MASV
OR dbo_sinhvien.MASV=dbo_doidangky.MASV
group by MASV";
$stmt = $db->prepare($sqlds);
$stmt->execute();
	
//============================ 
if(isset($_POST['submit'])){
if($stmt->execute()){
	while($r=$stmt->fetch()){
		$masv=htmlentities(trim(stripslashes($r['MASV'])));
		$email=htmlentities(trim(stripslashes($r['EMAIL'])));
		$subject=htmlentities(trim($_POST['subject']));
		$message="Hi! {$masv} \n \n";
		$message.=htmlentities($_POST['message']);
		$header="From: manhtuong.java@gmail.com";	
		$mailSent=mail($email,$subject,$message,$header); 
		echo $mailSent ? "<p class='success'>Email sent to ".$email."</p><br />" : "<p class='error'>Failed to sent email.</p>";
	}}}
?>

<?php require 'footer.php';?>
